package com.hainabo.mgcmall.api.system;

import com.hainabo.bootside.core.common.ReturnVO;
import com.hainabo.mgcmall.dao.AdvertisingPicturesRespository;
import com.hainabo.mgcmall.util.JsonUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by guoshihao on 2017/12/3.
 */
@Api(value = "/systemSetup")
@Component
@Path("/systemSetup")
@Produces(MediaType.APPLICATION_JSON)
@Slf4j
public class SystemSetup {

    @Autowired
    private AdvertisingPicturesRespository advertisingPicturesRespository;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @POST
    @Path("getImg")
    public ReturnVO getImg(String json){
        List list = (List) advertisingPicturesRespository.findAll();
        return new ReturnVO(list);
    }

    @POST
    @Path("getPriceInfo")
    public ReturnVO getPriceInfo(@Context HttpServletRequest request){
        String sql = "SELECT * FROM sys_price WHERE id = 1";
        Map price = jdbcTemplate.queryForMap(sql);
        return new ReturnVO(price);
    }

    @POST
    @Path("editImg")
    public ReturnVO editImg(String json){
        Map map = JsonUtil.toHashMap(json);
        if (!map.get("img1").toString().isEmpty()){
            StringBuilder sql1 = new StringBuilder();
            sql1.append("UPDATE `t_advertising_pictures` SET url ='"+map.get("img1").toString()+"'");
            sql1.append(" where id = 10000000 ");
            jdbcTemplate.update(sql1.toString());
        }
        if (!map.get("img2").toString().isEmpty()){
            StringBuilder sql2 = new StringBuilder();
            sql2.append("UPDATE `t_advertising_pictures` SET url ='"+map.get("img2").toString()+"'");
            sql2.append(" where id = 10000001 ");
            jdbcTemplate.update(sql2.toString());
        }
        if (!map.get("img3").toString().isEmpty()){
            StringBuilder sql3 = new StringBuilder();
            sql3.append("UPDATE `t_advertising_pictures` SET url ='"+map.get("img3").toString()+"'");
            sql3.append(" where id = 10000002 ");
            jdbcTemplate.update(sql3.toString());
        }
        return new ReturnVO(ReturnVO.CODE_SUCCESS);
    }

    @POST
    @Path("editPrice")
    public ReturnVO editPrice(String json){
        Map param = JsonUtil.toHashMap(json);
        String sql = "UPDATE " +
                "  `sys_price` " +
                " SET" +
                "  cross_distance = " +param.get("cross_distance").toString()+
                ",  cross_city = '" +param.get("cross_city").toString()+"'"+
                ",  cross_city_e = '" +param.get("cross_city_e").toString()+"'"+
                ",  c_distance = " +param.get("c_distance").toString()+
                ",  city = '" +param.get("city").toString()+"'"+
                ",  city_e = '" +param.get("city_e").toString()+"'"+
                " WHERE id = "+param.get("id");
        jdbcTemplate.update(sql);
        return new ReturnVO(ReturnVO.CODE_SUCCESS);
    }

}
